package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.MarketPointLogManager;
import com.fish.anne.model.MarketPointLog;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class MarketPointLogAction extends BaseAction implements Preparable {
    private MarketPointLogManager marketPointLogManager;
    private List marketPointLogs;
    private MarketPointLog marketPointLog;
    private Long  pointLogId;

    public void setMarketPointLogManager(MarketPointLogManager marketPointLogManager) {
        this.marketPointLogManager = marketPointLogManager;
    }

    public List getMarketPointLogs() {
        return marketPointLogs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String marketPointLogId = getRequest().getParameter("marketPointLog.pointLogId");
            if (marketPointLogId != null && !marketPointLogId.equals("")) {
                marketPointLog = marketPointLogManager.get(new Long(marketPointLogId));
            }
        }
    }

    public String list() {
        marketPointLogs = marketPointLogManager.getAll();
        return SUCCESS;
    }

    public void setPointLogId(Long  pointLogId) {
        this. pointLogId =  pointLogId;
    }

    public MarketPointLog getMarketPointLog() {
        return marketPointLog;
    }

    public void setMarketPointLog(MarketPointLog marketPointLog) {
        this.marketPointLog = marketPointLog;
    }

    public String delete() {
        marketPointLogManager.remove(marketPointLog.getPointLogId());
        saveMessage(getText("marketPointLog.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (pointLogId != null) {
            marketPointLog = marketPointLogManager.get(pointLogId);
        } else {
            marketPointLog = new MarketPointLog();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (marketPointLog.getPointLogId() == null);

        marketPointLogManager.save(marketPointLog);

        String key = (isNew) ? "marketPointLog.added" : "marketPointLog.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}